<?php
# @version SVN: $Id: select.class.inc 587 2012-04-26 10:34:01Z novachok $

require_once(ROOT . LIBRARY .'/fields/common.inc');

class fields_select extends fields_common
{
   static public $values_cached = array();

   // TODO: проверить - работет ли этот кеш - или нужно вынести переменную вне класса
   protected $source = 'vars';

   public function  __construct($data)
   {
      parent::__construct($data);
      $this->escaped_columns[] = 'src_value';
      $this->escaped_columns[] = 'values';
      $this->escaped_columns[] = 'source';

      switch($this->source)
      {
         case 'articles':
            if(!empty($this->values))
            {
               $this->cacheArticleValues($this->values);
               $this->values = self::$values_cached[$this->values];
            } else
            {
               $this->values = NULL;
            }
            break;

         case 'vars':
         default:
            $this->values = !empty($this->values) ? gettext::translate($this->values) : NULL;
            if(!empty($this->values) && is_array($this->values))
            {
               asort($this->values);
               $tmp = array();
               foreach($this->values AS $k=>$v)
               {
                  if($k{0} == '_') $tmp[substr($k, 1)] = $v;
                  else $tmp[$k] = $v;
               }
               $this->values = $tmp;
               unset($tmp);
            }
            break;
      }
   }


   /**
    * Normalize Value
    *
    * @param integer $value
    * @param string $lang
    */
   public function normalizeValue($value, $lang = null)
   {
      $this->src_value = NULL;

      if(!empty($this->values))
      {
         if(isset($this->values[$value]))
         {
            $this->src_value = $value;
            $this->value = $this->values[$value];
         }
      }
      
      return $this->value;
   }


   /**
    * Get HTML code for SELECT fields
    *
    * @param object $data
    * @return string
    */
   public function getFieldHTML($data)
   {
      $value = $data->{$this->alias}('source');
      $html = '';

      if(empty($this->values))
      {
         $html = '<span class="warn">'. gettext::translate('meccano', 'no_langvars') . '</span>';
      } else
      {
         $html .= '<select name="f['. $this->alias .']';
         $html .= '">';
         $html .= '<option value="">'. gettext::translate('meccano', 'not_set') .'</option>';

         foreach($this->values AS $k=>$v)
         {
            $html .= '<option value="'. $k .'"';
            if($k == $value) $html .= ' selected';
            $html .= '>'. $v .'</option>';
         }
         $html .= '</select>';
      }

      return $html;
   }


   /**
    * Validate Input Data agains field type
    * @param mixed $value
    * @param object $dataObject
    * @return mixed
    */
   public function validateInput($value, $dataObject = NULL)
   {
      if(!isset($this->values[$value]))
      {
         $this->value = $this->default;
      } else
      {
         $this->value = $value;
      }
      
      return $this->value;
   }

   /**
    * Cache SELECT values from articles
    * @param integer $id
    */
   private function cacheArticleValues($id)
   {
      if(empty(self::$values_cached[$id]))
      {
         $A = new articles();
         $A->setOrder('`title` ASC');
         $filter = array('field' => array(
            'cat' => array('type' => 'exact', 'value' => $id),
            'active' => array('type' => 'exact', 'value' => 1)
            ));
         $data = $A->getList($filter);
         if(!empty($data))
         {
            self::$values_cached[$id] = array();
            foreach($data AS $v)
            {
               self::$values_cached[$id][$v['id']] = $v['title'];
            }
         }
      }
   }
}